// pages/gift/gift.js
const util = require("../../utils/util.js");
var app = getApp();
const requestManager = require("../../utils/request.js");
var router = require('../../utils/router.js');
const pageName = "PersonalCenter "
var WxParse = require('../../wxParse/wxParse.js');
let optionsEbuyId = ''
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    isCart: null, // 是否隐藏按钮和倒计时
    detailData: null,
    day: 0,
    hour: 0,
    minute: 0,
    second: 0
  },
  goOrderConfirm() {
    let url = "/pages/orderconfirm/orderconfirm";
    router.route.setAction(router.NAVIGATE_TO)
      .setParams([`productId=${this.data.detailData.productId}&ebuyId=${optionsEbuyId}`])
      .setUrl(url)
      .run()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('options',options)
    this.setData({
      isCart:options.isCart
    })
    optionsEbuyId = options.ebuyId
    this.getDetaildata(options.productId, options.ebuyId)
  },
  //获取商品详情数据
  getDetaildata(productid, ebuyId) {
    wx.showLoading()
    let that = this
    let params = {
      userId: util.getUserInfo().userId,
      productId: productid,
      ebuyId: ebuyId,
      channel: 'wxapp'
    }
    requestManager.getDataResult(requestManager.product_detail, params, "GET").then(res => {
      this.setData({
        detailData: res
      })
      var article = res.productDes;
      WxParse.wxParse('article', 'html', article, that, 5);
      this.countDown(res.timestamp)
      wx.hideLoading()
    })
  },
  // 倒计时
  countDown(options) {
    var timer = null;
    let obj = this
    var day = 0,
      hour = 0,
      minute = 0,
      second = 0; //时间默认值
    var tTimer
    // var tTimer = times - parseInt(new Date().getTime() / 1000)
    timer = setInterval(function() {
      tTimer = options - parseInt(new Date().getTime() / 1000)

      if (tTimer > 0) {
        day = Math.floor(tTimer / (60 * 60 * 24));
        hour = Math.floor(tTimer / (60 * 60)) - (day * 24);
        minute = Math.floor(tTimer / 60) - (day * 24 * 60) - (hour * 60);
        second = Math.floor(tTimer) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
      }
      if (day <= 9 && day > 0) day = '0' + day;
      if (hour <= 9 && hour > 0) hour = '0' + hour;
      if (minute <= 9 && minute > 0) minute = '0' + minute;
      if (second <= 9 && second > 0) second = '0' + second;
      //
      obj.setData({
        day: day,
        hour: hour,
        minute: minute,
        second: second

      })
      // console.log(day + "天:" + hour + "小时：" + minute + "分钟：" + second + "秒");
      // times--;
    }, 1000);
    if (tTimer <= 0) {
      clearInterval(timer);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})